<script setup>
import { Plus } from '@element-plus/icons-vue'
import Selector from '@/components/QuestionSelector.vue'
import { delQuestionService, getQuestionListService } from '@/apis/question'
import { ref } from 'vue'
import QuestionDrawer from '@/components/QuestionDrawer.vue'

const params = ref({
  pageNum: 1,
  pageSize: 10,
  difficulty: null,
  title: ''
})

const questionList = ref([])
const total = ref(0)

const getQuestionList = async () => {
  const res = await getQuestionListService(params.value)
  questionList.value = res.rows
  total.value = res.total
}
getQuestionList()

const onSearch = () => {
  params.value.pageNum = 1
  getQuestionList()
}

const onReset = () => {
  params.value.pageNum = 1
  params.value.pageSize = 10
  params.value.title = ''
  params.value.difficulty = null
  getQuestionList()
}

const onSizeChange = () => {
  // params.value.pageSize = newSize
  params.value.pageNum = 1
  getQuestionList()
}

const onCurrentChange = () => {
  // params.value.pageNum = newPage
  getQuestionList()
}

const questionDrawerRef = ref(null)

const onAddQuestion = () => {
  questionDrawerRef.value.open()
}

const onSuccess = (service) => {
  if (service == 'add') {
    params.value.pageNum = 1
  }
  getQuestionList()
}

const onEdit = async (questionId) => {
  questionDrawerRef.value.open(questionId)
}

const onDelete = async (questionId) => {
  await delQuestionService(questionId)
  params.value.pageNum = 1
  getQuestionList()
}
</script>

<template>
  <!-- 搜索框 -->
  <el-form inline="true">
    <el-form-item>
      <selector
        v-model="params.difficulty"
        placeholder="请选择题目难度"
        style="width: 200px"
      ></selector>
    </el-form-item>
    <el-form-item>
      <el-input v-model="params.title" placeholder="请您输入要搜索的题目标题" />
    </el-form-item>
    <el-form-item>
      <el-button @click="onSearch()" plain>搜索</el-button>
      <el-button @click="onReset()" plain type="info">重置</el-button>
      <el-button plain type="primary" :icon="Plus" @click="onAddQuestion()">添加题目</el-button>
    </el-form-item>
  </el-form>

  <!-- 表格 -->
  <el-table height="480px" :data="questionList">
    <el-table-column prop="questionId" width="180px" label="题目id" />
    <el-table-column prop="title" label="题目标题" />
    <el-table-column prop="difficulty" label="题目难度" width="90px">
      <template #default="{ row }">
        <div v-if="row.difficulty === 1" style="color: #3ec8ff">简单</div>
        <div v-if="row.difficulty === 2" style="color: #fe7909">中等</div>
        <div v-if="row.difficulty === 3" style="color: #fd4c40">困难</div>
      </template>
    </el-table-column>
    <el-table-column prop="createName" label="创建人" width="140px" />
    <el-table-column prop="createTime" label="创建时间" width="180px" />
    <el-table-column label="操作" width="100px" fixed="right">
      <template #default="{ row }">
        <el-button type="text" @click="onEdit(row.questionId)">编辑 </el-button>
        <el-button type="text" class="red" @click="onDelete(row.questionId)">删除 </el-button>
      </template>
    </el-table-column>
  </el-table>

  <!-- 分页 -->
  <el-pagination
    background
    size="small"
    layout="total, sizes, prev, pager, next, jumper"
    v-model:current-page="params.pageNum"
    v-model:page-size="params.pageSize"
    :default-page-size="params.pageSize"
    :page-sizes="[5, 10, 15, 20]"
    :total="total"
    @size-change="onSizeChange"
    @current-change="onCurrentChange"
  />

  <question-drawer ref="questionDrawerRef" @success="onSuccess()"></question-drawer>
</template>
